package com.batchengine.pool;

import com.batchengine.BatchEngineException;
import com.batchengine.worker.Worker;
import static com.batchengine.util.LoggingFactory.createLogger;
import org.apache.log4j.Logger;

/**
 * Created by sunil.incubator@yahoo.com.
 * User: sunil.kalva
 * Date: Jul 19, 2009
 */
public class ExecutorWorkerPool extends WorkerPool {

    private static final Logger logger = createLogger();
    private boolean poolInitialized = false;

    protected ExecutorWorkerPool(int minWorkers, int maxWorkers, long taskId, WorkerFactory workerFactory) {
        super(minWorkers, maxWorkers, workerFactory);
        setTaskId(taskId);
    }

    protected ExecutorWorkerPool(String name, int minWorkers, int maxWorkers, long taskId, WorkerFactory workerFactory) {
        super(name, minWorkers, maxWorkers, workerFactory);
        setTaskId(taskId);
    }

    protected ExecutorWorkerPool(String name, long taskId, WorkerFactory workerFactory) {
        super(name, workerFactory);
        setTaskId(taskId);
    }

    protected ExecutorWorkerPool(long taskId, WorkerFactory workerFactory) {
        super(workerFactory);
        setTaskId(taskId);
    }

    protected ExecutorWorkerPool(int minWorkers, int maxWorkers, long taskId) {
        super(minWorkers, maxWorkers);
        setTaskId(taskId);
    }

    protected ExecutorWorkerPool(int minWorkers, int maxWorkers, long taskId, String name) {
        super(minWorkers, maxWorkers, name);
        setTaskId(taskId);
    }


    @Override
    public synchronized void startWorkers() {

        if (poolInitialized) {
            throw new BatchEngineException("Pool already started");
        }

        //start the min number of instant threads.
        for (int i = 1; i <= minWorkers; i++) {
            Worker s = addWorker();
            logger.debug(String.format("worker %s  created", s));
        }
        if (null != getSignal()) {
            logger.debug("@@@@@@@@ starting worker killer @@@@@@@@");
            getSignal().start();
        }
    }
}
